package de.dopichaj.labrador.search.own;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.dopichaj.labrador.index.Index;
import de.dopichaj.labrador.index.backend.Term;
import de.dopichaj.labrador.index.backend.TermFrequency;
import de.dopichaj.labrador.search.hit.Hit;
import de.dopichaj.labrador.util.Configuration;


public final class OkapiBM25SimilarityFactory implements SimilarityFactory {

    private final double b;
    private final double k1;
    private final int elementCount;
    private final int averageElementLength;
    
    public OkapiBM25SimilarityFactory(final double k1, final double b,
        final int elementCount, final int averageElementLength) {
        
        this.b = b;
        this.k1 = k1;
        this.elementCount = elementCount;
        this.averageElementLength = averageElementLength;
    }

    public OkapiBM25SimilarityFactory(final Configuration configuration,
        final int elementCount, final int averageElementLength) {
        
        this(configuration.getDouble("okapi.k1", 1.2),
            configuration.getDouble("okapi.b", 0.75),
            elementCount, averageElementLength);

    }
    
    public OkapiBM25SimilarityFactory(final Index index) {
        
        this(index.getConfiguration(),
            index.getElementCount(),
            (int)(index.getElementLengthSum() / index.getElementCount()));
    }

    public Similarity getSimilarity(final OwnQuery query) {
        return new OkapiBM25Similarity(query);
    }

    /**
     * Okapi BM25 similarity function; uses the constants from the factory.
     */
    private final class OkapiBM25Similarity implements Similarity {

        private final Map<Integer, Double> termWeights;
        
        private OkapiBM25Similarity(final OwnQuery query) {
            termWeights = new HashMap<Integer, Double>();
            for (final Term term : query.getQueryTerms()) {
                
                final int documentFrequency = term.getDocumentFrequency();
                termWeights.put(term.getID(),
                    Math.log((elementCount - documentFrequency + 0.5) /
                        (documentFrequency + 0.5)));
            }
        }
        
        public double similarity(Hit hit, List<TermFrequency> frequencyList) {
            
            final double lengthNorm =
                k1 * ((1-b) + b * hit.getContentLength() / averageElementLength);
            double elitenessSum = 0.0;
            
            for (final TermFrequency freq : frequencyList) {
                final int tf = freq.getFrequency();
                elitenessSum += ((k1 + 1) * tf) / (lengthNorm + tf) *
                    termWeights.get(freq.getTermID());
            }
            return elitenessSum;
        }

    }

    public String getDescription() {
        return "Okapi BM25 similarity (k1=" + k1 + ", b=" + b + ")";
    }
}
/*
Copyright (c) 2005-2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/